package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.RoleMapper;
import com.qingcheng.dao.RoleResourceMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.system.Resource;
import com.qingcheng.pojo.system.Role;
import com.qingcheng.pojo.system.RoleResource;
import com.qingcheng.pojo.system.WholeRole;
import com.qingcheng.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Role> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Role> roles = (Page<Role>) roleMapper.selectAll();
        return new PageResult<Role>(roles.getTotal(),roles.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Role> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return roleMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Role> roles = (Page<Role>) roleMapper.selectByExample(example);
        return new PageResult<Role>(roles.getTotal(),roles.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Role findById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param role
     */
    public void add(Role role) {
        roleMapper.insert(role);
    }

    /**
     * 修改
     * @param role
     */
    public void update(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Integer id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 定义查询所有角色和其资源的方法
     * @return
     */
    @Override
    public List<WholeRole> findAllWholeRole() {
        //创建List集合，用来作为返回值
        List<WholeRole> wholeRoleList=new ArrayList<WholeRole>();
        //查询所有角色的集合
        List<Role> roleList = roleMapper.selectAll();
        //遍历集合，拿到每个角色的id
        if(roleList.size()!=0){
            for (Role role : roleList) {
                //调用方法，根据角色的id查询所有资源的集合
                Integer roleId = role.getId();
                List<Resource> resourceList = roleMapper.findResourceByRoleId(roleId);
                //创建WholeRole对象，并封装查询到的数据
                WholeRole wholeRole=new WholeRole();
                wholeRole.setRole(role);
                wholeRole.setResourceList(resourceList);
                //将wholeRole对象封装进list集合
                wholeRoleList.add(wholeRole);
            }
        }

        //返回集合
        return wholeRoleList;
    }

    /**
     * 定义添加角色的方法
     * @param wholeRole
     */
    @Override
    @Transactional
    public void addWholeRole(WholeRole wholeRole) {
        //获取角色对象
        Role role = wholeRole.getRole();
        //将角色存入数据库
        roleMapper.insert(role);
        //获取角色对象的id
        Integer roleId = role.getId();
        //获取Resource集合
        List<Resource> resourceList = wholeRole.getResourceList();
        //遍历集合，获取每个resource的id
        if(resourceList.size()!=0){
            for (Resource resource : resourceList) {
                //创建RoleResource对象
                RoleResource roleResource=new RoleResource();
                //将Role的id和Resource的id封装进RoleResource对象中
                roleResource.setRoleId(roleId);
                roleResource.setResourceId(resource.getId());
                //将RoleResource对象存入数据库
                roleResourceMapper.insert(roleResource);
            }
        }
    }

    /**
     * 定义修改角色信息的方法
     * @param wholeRole
     */
    @Override
    @Transactional
    public void updateWholeRole(WholeRole wholeRole) {
        //获取角色对象
        Role role = wholeRole.getRole();
        //将角色信息存入数据库
        roleMapper.updateByPrimaryKeySelective(role);
        //获取角色的Id
        Integer roleId = role.getId();
        //根据角色Id删除RoleResource表中的数据
        Example example=new Example(RoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",roleId);
        roleResourceMapper.deleteByExample(example);
        //将角色对应的ResourceRole信息遍历存入数据库
        List<Resource> resourceList = wholeRole.getResourceList();
        if(resourceList.size()!=0){
            for (Resource resource : resourceList) {
                RoleResource roleResource=new RoleResource();
                roleResource.setRoleId(roleId);
                roleResource.setResourceId(resource.getId());
                //将数据存入数据库
                roleResourceMapper.insert(roleResource);
            }
        }
    }

    /**
     * 定义删除用户的方法
     * @param id
     */
    @Override
    @Transactional
    public void deleteWholeRole(Integer id) {
        //根据id删除Role
        roleMapper.deleteByPrimaryKey(id);
        //根据id删除RoleResource
        Example example=new Example(RoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",id);
        roleResourceMapper.deleteByExample(example);
    }

    /**
     * 定义根据ID查询一个完整角色的方法
     * @param id
     */
    @Override
    public WholeRole findWholeRoleById(Integer id) {
        //根据id查询角色
        Role role = roleMapper.selectByPrimaryKey(id);
        //根据id查询对应的所有资源集合
        List<Resource> resourceList = roleMapper.findResourceByRoleId(id);
        //将数据封装进WholeRole对象中
        WholeRole wholeRole=new WholeRole();
        wholeRole.setRole(role);
        wholeRole.setResourceList(resourceList);
        return wholeRole;
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 角色名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            // ID
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }

        }
        return example;
    }

}
